import React, { PureComponent, Fragment } from 'react';
import { Table, Alert } from 'antd';
import styles from './index.less';

function initTotalList(columns) {
    const totalList = [];
    columns.forEach((column) => {
        if (column.needTotal) {
            totalList.push({ ...column, total: 0 });
        }
    });
    return totalList;
}

class StandardTable extends PureComponent {
    constructor(props) {
        super(props);
        const { columns } = props;
        const needTotalList = initTotalList(columns);
        this.state = {
            needTotalList,
        };
    }

    componentWillReceiveProps(nextProps) {
        // clean state
        if (nextProps.selectedRows.length === 0) {
            const needTotalList = initTotalList(nextProps.columns);
            this.setState({
                needTotalList,
            });
        }
    }

    handleTableChange = (pagination, filters, sorter) => {
        this.props.onChange(pagination, filters, sorter);
    };

    render() {
        const { data: { content, page, size, totalElements }, loading, columns, expandedRowRender, searchForm } = this.props;
        const paginationProps = {
            showSizeChanger: true,
            showQuickJumper: true,
            current: page,
            pageSize: size,
            total: totalElements,
            searchForm,
        };
        return (
            <Table
                className="components-table-demo-nested"
                loading={loading}
                rowKey={record => record.id}
                dataSource={content}
                columns={columns}
                expandedRowRender={expandedRowRender}
                pagination={paginationProps}
                onChange={this.handleTableChange}
                expandRowByClick={true}
            />
        );
    }
}

export default StandardTable;
